var ctx=window.daijia.ctx;
var errandyear,errandmonths,errandcompanyId;
var errandxdData = new Array(12);
var errandcjData = new Array(12);
var errandjeData = new Array(12);
var errandMonth="";
var errandDay="";
var errandHour="";
$(function(){
	
	function tableload(){
		$.ajax({
			
	        url : ctx+'/chart/errandTableOrderMonthData',
	        type:'GET',
	        async:true,
	        dataType : 'json',
	        data : {
	        
	        	"year":$("#errandYear").val(),
	        	"companyId":$("#errandCompanyId").val()
	        },
	        success : function(result){
	        	errandMonth="";
	        	errandDay="";
	        	errandHour="";
	        	var head = "<tr style='font-size:14px;'><th style='width:90px;'>分类\\月份</th><th>1</th><th>2</th><th>3</th><th>4</th><th>5</th><th>6</th><th>7</th><th>8</th><th>9</th><th>10</th><th>11</th><th>12</th></tr>";
	        	var tr1='<td>销单量</td>';
	        	var tr2='<td>完成单量</td>';
	        	var tr3='<td>金额(元)</td>';
	        	$("#errandHead tr").remove();
	        	$("#errandHead").append(head);
	        	if(result.length>0){
	        newcontent+="<tbody style='display:none;'>";		
	        		$(result).each(function(index,data){
	        		//"<td><a href='"+ctx+"/chart/tableOrderDay?"+"year="+$("#year").val()+"&month="+data.time+"&companyId="+companyId+"' target='_blank'>"+data.time+"</a></td>"+	
		    tr1+="<td>"+data.cancelAmount+"</td>";
		    tr2+="<td>"+data.finishedAmount+"</td>";
		    tr3+="<td>"+data.money+"</td>";
	        		});
	       newcontent+="</tbody>";		
	        	}
	        	var newcontent = "<tr>"+tr1+"</tr><tr>"+tr2+"</tr><tr>"+tr3+"</tr>";
	        	$('#loadblacklayer').remove();
	        	$("#errandTable tbody").remove();
	        	$("#errandTable").append(newcontent);
	        	$("#errandTable tbody").show(300);
	    	}
		});
	}
	
	tableload();
	
	$("#errand_export_button").click(function(){
		window.location=ctx+"/chart/errandExport?exportYear="+$("#errandYear").val()+(isMain?"&exportCompanyId="+$("#errandCompanyId").val():"")+
		                "&exportMonth="+errandMonth+"&exportDay="+errandDay;
	});
	$("#errand_search_button").click(function(){
		$("#errand").css("width","100%");
		var y = $("#errandYear").val();
		var c=$("#errandCompanyId").val();
		var cname =$('#errandCompanyId').find("option:selected").text();
		 errandyear = $("#errandYear").val();
		 errandcompanyId = $("#errandCompanyId").val();
		 if($('.erloadblacklayer').length==0){
				var dw = $("#errandTab").width();
				var dh = $("#errandTab").height();
				var backlayer ="<div class='erloadblacklayer'><div style='margin-top:"+(dh/2)+"px;margin-left:"+(dw/2)+"px;'><img src='"+ctx+"/assets/css/images/loading.gif' /></div></div>"
				$("#errandTab").append(backlayer);
				$('.erloadblacklayer').css({"z-index":"12","background":"#000","opacity":"0.5","position":'absolute',"top":0,"width":dw,"height":dh});
			}
		 
		$.post(window.daijia.ctx+'/chart/errandDataForYear',{'year':y,'companyId':c},function(data){
			$('.erloadblacklayer').remove();
			errandmonths = data.months;
			
			if(errandmonths){
				for(var i=0 ; i<errandmonths.length ; i++){
					var month = errandmonths[i];
					errandxdData[month.month] = month.totalCancelNum;
					errandcjData[month.month] = month.totalSuccessNum;
					errandjeData[month.month] = month.totalPrice;
				}
				tableload();
				errandRenderYearChart(errandxdData,errandcjData,errandjeData,errandmonths);
			}else{
				$.scojs_message('公司  '+cname+' 没有订单记录', $.scojs_message.TYPE_OK);
			}
			
		},'json');
		
	});
	Highcharts.setOptions({
		lang:{
			printChart:'打印报表',
			downloadJPEG:'保存为JPG图片',
			downloadPDF: '保存为PDF文档',
			downloadPNG: '保存为PNG图片',
			downloadSVG: '保存为SVG文档',
			loading:'加载中...',
			resetZoom:'重置缩放',
			resetZoomTitle:'缩放比例：1:1',
			months:['1月', '2月', '3月', '4月', '5月', '6月','7月', '8月', '9月', '10月', '11月', '12月']
		}
	});
	
	
	
	 errandyear = $("#errandYear").val();
	 errandcompanyId = $("#errandCompanyId").val();
	
	for(var i=0 ; i<12 ; i++){
		errandxdData[i] = 0;
		errandcjData[i] = 0;
		errandjeData[i] = 12;
	}
	
	
	$.post(window.daijia.ctx+'/chart/errandDataForYear',{'year':errandyear,'companyId':errandcompanyId},function(data){
		
		errandmonths = data.months;
		
		if(errandmonths){
			for(var i=0 ; i<errandmonths.length ; i++){
				
				
				var month = errandmonths[i];
				errandxdData[month.month] = month.totalCancelNum;
				errandcjData[month.month] = month.totalSuccessNum;
				errandjeData[month.month] = month.totalPrice;
			}
			
			errandRenderYearChart(errandxdData,errandcjData,errandjeData,errandmonths);
		}else{
			$.scojs_message('没有订单记录', $.scojs_message.TYPE_OK);
		}
		
	},'json');
	
	/*$("#year").change(function(){
		$("#submit_form").submit();
	});
	
	$("#companyId").change(function(){
		$("#submit_form").submit();
	});*/
	
});

errandRenderYearChart=function(xdData,cjData,jeData,months){
	$("#errand").css("width",$(".container-fluid .navigation").width());
	$("#errand").highcharts({
		chart: {
            renderTo: 'errand',
            zoomType: 'xy'
        },
		plotOptions: {
			series:{
				cursor: 'pointer',
	            events:{
	            	click:function(e){
	            		$("#errand").css("width","100%");
	            		var category = e.point.category;
	            		var month = parseInt(category.substring(0,category.length-1))-1;
	            		errandMonth=month;
	            		
	            		$.post(window.daijia.ctx+'/chart/errandDataForMonth',{'year':$("#errandYear").val(),'month':month,'companyId':$("#errandCompanyId").val()},function(data){
	            			var dataHours = data.hours;
	            			var hours = new Array();
	            			var days=data.chartDays;
	            			var xdmData = new Array();
	            			var cjmData = new Array();
	            			var jemData = new Array();
	            			var head = "<tr style='font-size:14px;'><th style='width:90px;'>分类\\天</th>";
	            			var tr1='<td>销单量</td>';
	            			var tr2='<td>完成单量</td>';
	            			var tr3='<td>金额(元)</td>';
	            			for(var i = 0 ; i < days.length ; i++){
	            				var chartDay = days[i];
	            				days[i] = chartDay.day;
	            				xdmData[i] = chartDay.totalCancelNum;
	            				cjmData[i] = chartDay.totalSuccessNum;
	            				jemData[i] = chartDay.totalPrice;
	            				head+="<th>"+chartDay.day+"</th>";
	            				tr1+="<td>"+chartDay.totalCancelNum+"</td>";
	            				tr2+="<td>"+chartDay.totalSuccessNum+"</td>";
	            				tr3+="<td>"+chartDay.totalPrice+"</td>";
	            			}
	            			head=head+"</tr>"
	            			var newcontent = "<tr>"+tr1+"</tr><tr>"+tr2+"</tr><tr>"+tr3+"</tr>";
	            			$("#errandHead tr").remove();
	            			$("#errandHead").append(head);
	            			$("#errandTable tbody").remove();
	            			$("#errandTable").append(newcontent);
	            			$("#errandTable tbody").show(300);
	            			errandRenderMonthChart(errandyear,month,days,xdmData,cjmData,jemData);
	            			
	            		},'json');
/*	            		var category = e.point.category;
	            		//加载月数据 
	            		var year = $("#year").val();
	            		var month = parseInt(category.substring(0,category.length-1))-1;
	            		var days = new Array();
	            		var xdmData = new Array();
	            		var cjmData = new Array();
	            		var jemData = new Array();
	            		
            			var currentMonth = months[month];
            			var days = currentMonth.chartDays;
            			for(var i=0;i<mouths.length;i++){
            				
            				xdmData[i]=mouths[i].totalCancelNum;
            				cjData[i] = mouths[i].totalSuccessNum;
            				jemData[i]=mouths[i].totalPrice;
            				
            			}
            			
            			
            			for(var i = 0 ; i < days.length ; i++){
            				var chartDay = days[i];
            				days[i] = chartDay.day;
            				xdmData[i] = chartDay.totalCancelNum;
            				cjmData[i] = chartDay.totalSuccessNum;
            				jemData[i] = chartDay.totalPrice;
            			}
            			
	            		
            			errandRenderMonthChart(year,month,days,xdmData,cjmData,jemData);*/
	            	}
	            }
			}
        },
        credits:{
        	enabled:false
        },
        title: {
            text: '跑腿订单统计图'
        },
        subtitle: {
            text: errandyear+'年'
        },
        xAxis: [{
            categories: Highcharts.getOptions().lang.months
        }],
        yAxis: [{ // Primary yAxis
            labels: {
                format: '{value}单',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            title: {
                text: '单量',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            allowDecimals: false
        }, { // Secondary yAxis
            title: {
                text: '总价',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            labels: {
                format: '{value} 元',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            opposite: true
        }],
        tooltip: {
            shared: true
        },
        legend: {
            layout: 'vertical',
            align: 'left',
            x: 120,
            verticalAlign: 'top',
            y: 100,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
        },
        series: [{
            name: '金额',
            type: 'column',
            yAxis: 1,
            data: jeData,
            tooltip: {
                valueSuffix: '元'
            }

        }, {
            name: '单量',
            type: 'spline',
            data: cjData,
            tooltip: {
                valueSuffix: '单'
            }
        }, {
            name: '已销单',
            type: 'spline',
            data: xdData,
            tooltip: {
                valueSuffix: '个'
            }
        }]
    });
	
};


errandRenderMonthChart=function(year,month,days,xdData,cjData,jeData){
	
	$("#errand").highcharts({
		chart: {
            renderTo:  'errand',
            zoomType: 'xy'
        },
		plotOptions: {
			series:{
				cursor: 'pointer',
	            events:{
	            	click:function(e){
	            		var category = e.point.category;
	            		//加载某天数据 
	            		errandDay=category;
	            		$.post(window.daijia.ctx+'/chart/errandDataForDay',{'year':year,'month':month,'day':category,'companyId':$("#errandCompanyId").val()},function(data){
	            			var dataHours = data.hours;
	            			var hours = new Array();
	            			
	            			var xddData = new Array();
	            			var cjdData = new Array();
	            			var jedData = new Array();
	            			var head = "<tr style='font-size:14px;'><th style='width:90px;'>分类\\小时</th>";
	            			var tr1='<td>销单量</td>';
	            			var tr2='<td>完成单量</td>';
	            			var tr3='<td>金额(元)</td>';
	            			for(var i=0 ; i<dataHours.length ; i++){
	            				var hour = dataHours[i];
	            				hours[i] = hour.hour;
	            				xddData[i] = hour.totalCancel;
	            				cjdData[i] = hour.totalSuccess;
	            				jedData[i] = hour.totalPrice;
	            				head+="<th>"+hour.hour+"</th>";
	            				tr1+="<td>"+hour.totalCancel+"</td>";
	            				tr2+="<td>"+hour.totalSuccess+"</td>";
	            				tr3+="<td>"+hour.totalPrice+"</td>";
	            			}
	            			head=head+"</tr>"
	            			var newcontent = "<tr>"+tr1+"</tr><tr>"+tr2+"</tr><tr>"+tr3+"</tr>";
	            			$("#errandHead tr").remove();
	            			$("#errandHead").append(head);
	            			$("#errandTable tbody").remove();
	            			$("#errandTable").append(newcontent);
	            			$("#errandTable tbody").show(300);
	            			
	            			errandRenderDayChart(year,month,category,hours,xddData,cjdData,jedData);
	            			
	            		},'json');
	            		
	            	}
	            }
			}
        },
        credits:{
        	enabled:false
        },
        title: {
            text: '跑腿订单统计图'
        },
        subtitle: {
            text: errandyear+"年-"+(month+1)+"月"
        },
        xAxis: [{
            categories: days
        }],
        yAxis: [{ // Primary yAxis
            labels: {
                format: '{value}单',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            title: {
                text: '单量',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            allowDecimals: false
        }, { // Secondary yAxis
            title: {
                text: '总价',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            labels: {
                format: '{value} 元',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            opposite: true
        }],
        tooltip: {
            shared: true
        },
        legend: {
            layout: 'vertical',
            align: 'left',
            x: 120,
            verticalAlign: 'top',
            y: 100,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
        },
        series: [{
            name: '金额',
            type: 'column',
            yAxis: 1,
            data: jeData,
            tooltip: {
                valueSuffix: '元'
            }

        }, {
            name: '单量',
            type: 'spline',
            data: cjData,
            tooltip: {
                valueSuffix: '单'
            }
        }, {
            name: '已销单',
            type: 'spline',
            data: xdData,
            tooltip: {
                valueSuffix: '个'
            }
        }]
    });
	
};

errandRenderDayChart=function(year,month,day,hours,xddData,cjdData,jedData){
	
	$("#errand").highcharts({
		chart: {
            renderTo: 'errand',
            reflow:true,
            zoomType: 'xy'
        },
		plotOptions: {
			series:{
				cursor: 'pointer',
	            events:{
	            	click:function(e){
	            		//TODO 返回年数据统计
	            		errandMonth="";
	    	        	errandDay="";
	    	        	errandHour="";
	            		var head = "<tr style='font-size:14px;'><th style='width:90px;'>分类\\月份</th>";
            			var tr1='<td>销单量</td>';
            			var tr2='<td>完成单量</td>';
            			var tr3='<td>金额(元)</td>';
	            		for(var i=0;i<errandmonths.length;i++){
	            			head+="<th>"+((errandmonths[i].month)+1)+"</th>";
	            			tr1+="<td>"+(errandmonths[i]).totalCancelNum+"</td>";
            			    tr2+="<td>"+(errandmonths[i]).totalSuccessNum+"</td>";
            			    tr3+="<td>"+(errandmonths[i]).totalPrice+"</td>";
	            		}
	            		head=head+"</tr>"
            			var newcontent = "<tr>"+tr1+"</tr><tr>"+tr2+"</tr><tr>"+tr3+"</tr>";
            			$("#errandHead tr").remove();
            			$("#errandHead").append(head);
            			$("#errandTable tbody").remove();
            			$("#errandTable").append(newcontent);
            			$("#errandTable tbody").show(300);
	            		errandRenderYearChart(errandxdData,errandcjData,errandjeData,errandmonths);
	            	}
	            }
			}
        },
        credits:{
        	enabled:false
        },
        title: {
            text: '跑腿订单统计图'
        },
        subtitle: {
            text: errandyear+"年-"+(month+1)+"月-"+day+"日"
        },
        xAxis: [{
            categories: hours
        }],
        yAxis: [{ // Primary yAxis
            labels: {
                format: '{value}单',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            title: {
                text: '单量',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            allowDecimals: false
        }, { // Secondary yAxis
            title: {
                text: '总价',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            labels: {
                format: '{value} 元',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            opposite: true
        }],
        tooltip: {
            shared: true
        },
        legend: {
            layout: 'vertical',
            align: 'left',
            x: 120,
            verticalAlign: 'top',
            y: 100,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
        },
        series: [{
            name: '金额',
            type: 'column',
            yAxis: 1,
            data: jedData,
            tooltip: {
                valueSuffix: '元'
            }

        }, {
            name: '单量',
            type: 'spline',
            data: cjdData,
            tooltip: {
                valueSuffix: '单'
            }
        }, {
            name: '已销单',
            type: 'spline',
            data: xddData,
            tooltip: {
                valueSuffix: '个'
            }
        }]
    });
	
	
	
	
};


